package com.shopmall.common.config;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class RequestInterceptor implements HandlerInterceptor {
    private String[] passPath={
            "/api/front/login/mobile",
            "/api/front/sendCode",
            "/api/front/logout"
    };
    @Resource
    RedisTemplate redisTemplate;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("Authori-Zation");
        String requestURI = request.getRequestURI();
        String token1 =(String) redisTemplate.opsForValue().get("token");
        for (int i = 0; i < passPath.length; i++) {
            if (requestURI.equals(passPath[i])){
                return true;
            }
        }
        if (token1!=null||token1.equals(token)){
            response.getWriter().write("未登录或token不匹配！！");
            return false;
        }
       return true;
    }
}
